/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
/*
    This file list all the functions for user to implement.
*/
#include "nds03_def.h"
#include "nds03_platform.h"

#include "rtthread.h"
#include <rtdevice.h>
#include <pin.h>

#define DBG_TAG "ND03"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
static struct rt_i2c_bus_device *nd03_i2c_bus = RT_NULL; /* I2C总线设备句柄 */

/**
 * @brief NDS03使用平台初始化
 *
 * @param   pDev        平台设备指针
 * @param   arg         参数指针
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_platform_init(NDS03_Platform_t *pdev, void *arg){

    nd03_i2c_bus = (struct rt_i2c_bus_device *)rt_device_find("i2c2");
    if(nd03_i2c_bus == RT_NULL)
    {
        LOG_E("i2c2 bus not found!\n");
        return -1;
    }
    return 0;
}

/**
 * @brief NDS03使用平台释放
 *
 * @param   pDev        平台设备指针
 * @param   arg         参数指针
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_platform_uninit(NDS03_Platform_t *pdev, void *arg){
    return 0;
}

/**
 * @brief i2c连续读len个字节
 *
 * @param   pDev        平台设备指针
 * @param   reg_addr    读寄存器地址
 * @param   read_data   读数据
 * @param   len         读数据长度
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_i2c_read_nbytes(NDS03_Platform_t *pDev,  uint8_t reg_addr, uint8_t *read_data, uint16_t len)
{
    uint8_t i2c_addr = pDev->i2c_dev_addr;

    // 用户实现I2C读操作代码，注意restart之前没有stop位
    struct rt_i2c_msg i2c_msgs[2];
	i2c_msgs[0].addr = i2c_addr;
	i2c_msgs[0].buf = &reg_addr;
	i2c_msgs[0].len = 1;
	i2c_msgs[0].flags = RT_I2C_WR;

	i2c_msgs[1].addr = i2c_addr;
	i2c_msgs[1].buf = read_data;
	i2c_msgs[1].len = len;
	i2c_msgs[1].flags = RT_I2C_RD;

	if (rt_i2c_transfer(nd03_i2c_bus, i2c_msgs, 2) != 2)
	{
		return -1;
	}
    return 0;
}

/**
 * @brief i2c连续写len个字节
 *
 * @param   pDev        平台设备指针
 * @param   reg_addr    写寄存器地址
 * @param   write_data  写数据
 * @param   len         写数据长度
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_i2c_write_nbytes(NDS03_Platform_t *pDev,  uint8_t reg_addr, uint8_t *write_data, uint16_t len)
{
    uint8_t i2c_addr = pDev->i2c_dev_addr;

    // 用户实现I2C写操作代码
    struct rt_i2c_msg i2c_msgs[2];

	i2c_msgs[0].addr = i2c_addr;
	i2c_msgs[0].buf = &reg_addr;
	i2c_msgs[0].len = 1;
	i2c_msgs[0].flags = RT_I2C_WR;

	i2c_msgs[1].addr = i2c_addr;
	i2c_msgs[1].buf = write_data;
	i2c_msgs[1].len = len;
	i2c_msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;

	if (rt_i2c_transfer(nd03_i2c_bus, i2c_msgs, 2) != 2)
	{
		return -1;
	}
    return 0;
}

/**
 * @brief 延时wait_ms毫秒
 *
 * @param   pDev        平台设备指针
 * @param   wait_ms     输入需要延时时长
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_delay_1ms(NDS03_Platform_t *pDev, uint32_t wait_ms)
{
    // 用户实现延迟1ms代码
    rt_thread_mdelay(wait_ms);
    return 0;
}

/**
 * @brief 延时10*wait_10us微秒
 *
 * @param   pDev        平台设备指针
 * @param   wait_10us   输入需要延时时长
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_delay_10us(NDS03_Platform_t *pDev, uint32_t wait_10us)
{
    // 用户实现延迟10us代码
    rt_thread_delay(1);
    return 0;
}

/**
 * @brief 设置nds03 xshut引脚电平
 *
 * @param   pDev        平台设备指针
 * @param   level       引脚电平，0为低电平，1为高电平
 * @return  int8_t
 * @retval  0:成功, 其他:失败
 */
int8_t nds03_set_xshut_pin_level(NDS03_Platform_t *pDev, int8_t level)
{
    rt_pin_mode(pDev->xshut_pin, PIN_MODE_OUTPUT);
    rt_pin_write(pDev->xshut_pin, level);
    return 0;
}


